//package com.paypalpay.timingtask;
//
//import com.paypalpay.entity.common.DeviceRealTimeInformation;
//import com.paypalpay.entity.common.UnifiedOrderPackages;
//import com.paypalpay.entity.common.UserboundPaymentOrder;
//import com.paypalpay.pojo.Constant;
//import com.paypalpay.pojo.request.RequestBody.OpenMonthlyPackageRequestBody;
//import com.paypalpay.pojo.request.RequestHeader;
//import com.paypalpay.pojo.request.subRequest.OpenMonthlyPackageRequest;
//import com.paypalpay.pojo.response.subResponse.OpenMonthlyPackageResponse;
//import com.paypalpay.service.PartnerService;
//import com.paypalpay.service.UnifiedOrderPackagesService;
//import com.paypalpay.service.UserboundPaymentOrderService;
//import com.paypalpay.turntointerfaceservice.TurnToQueryDeviceRealTimeService;
//import com.paypalpay.turntointerfaceservice.utils.TurntoInterfaceUtils;
//import com.paypalpay.util.JaxbUtil;
//import com.paypalpay.util.wx.HttpClientTool;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//
///**
// * 用户选择订购并开通还是要主动去开通
// * 算tm什么订购并开通！
// *
// * @author lm
// * @date 2018/09/9
// */
//@Configuration
//@EnableScheduling
//public class OpenPackageTask {
//    @Autowired
//    private UserboundPaymentOrderService userboundPaymentOrderService;
//    @Autowired
//    private UnifiedOrderPackagesService unifiedOrderPackagesService;
//    @Autowired
//    PartnerService partnerService;
//
//    // @Scheduled(cron = "0 0 0/2 * * ?") //每两小时整点执行一次
//    //@Scheduled(cron = "0 0 0/1 * * ?") // 每1分钟执行一次
//    public void openTask() {
//        try {
//            // 2,生成当前时间戳
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String curtime = df.format(new Date());
//            //在数据库获取数据
//            System.out.println("OpenPackageTask开通定时任务正在执行！");
//            List<UserboundPaymentOrder> userList = userboundPaymentOrderService.getListToOpen();
//            if (userList == null || userList.isEmpty()) {
//                System.out.println("没有查询到需要去开通的数据！");
//            } else {
//
//                for (UserboundPaymentOrder order : userList) {
//                    String orderId = order.getOrderId();
//                    String deviceId = order.getDeviceId();
//                    String deviceType = order.getDeviceType();
//                    String partnerCode = order.getPartnerCode();
//                    String secret = partnerService.findSecretByPartnerCode(partnerCode);
//
//
//                    //1,先去查询一下当前设备是不是具有OTA功能的
//                    TurnToQueryDeviceRealTimeService queryDeviceRealTime = new TurnToQueryDeviceRealTimeService();
//                    DeviceRealTimeInformation information = queryDeviceRealTime.queryDeviceRealTime(deviceId, deviceType, secret, partnerCode);
//
//                    if (information == null) {
//                        System.out.println("请求未响应");
//                        return;
//                    } else {
//                        System.out.println("请求至少响应了");
//
//                        if (!true == Constant.RET_CODE_0000.equalsIgnoreCase(information.getRetCode())) {
//                            System.out.println("响应的内容可能有问题：" + information.getResultInfo());
//                        } else {
//                            System.out.println("请求至少响应:" + information.getResultInfo());
//                            //判断是否有ota功能
//                            if (information.getOtaFlag().equals("1")) {
//                                System.out.println("有ota功能的设备，不支持开通！");
//                                return;
//
//                            } else if (information.getOtaFlag().equals("2")) {
//                                System.out.println("没有ota功能的设备，支持开通");
//
//
//
//                                //请求参数处理
//                                OpenMonthlyPackageRequestBody body = new OpenMonthlyPackageRequestBody();
//                                body.setDeviceType(deviceType);
//                                body.setOrderId(orderId);
//
//                                // 请求参数拼接
//                                OpenMonthlyPackageRequest request = new OpenMonthlyPackageRequest();
//                                TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
//                                RequestHeader header = utils.getRequestHeader(Constant.cmd_openPackage, partnerCode);
//                                request.setHeader(header);
//                                request.setBody(body);
//                                String sign = utils.getSign(header, body, body.getClass(), secret);
//                                request.setSign(sign);
//
//
//                                // 将参数转换成XML格式
//                                String sRequest = new JaxbUtil(OpenMonthlyPackageRequest.class).toXml(request, "utf-8");
//                                System.out.println("请求报文：" + sRequest);
//
//                                // 发送post请求，并接收返回结果
//                                String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
//                                System.out.println("响应报文：" + result);
//                                OpenMonthlyPackageResponse responseObj = new JaxbUtil(OpenMonthlyPackageResponse.class).fromXml(result, "utf-8");
//                                String retCode = responseObj.getHeader().getResultCode();
//                                String resultInfo = responseObj.getHeader().getResultInfo();
//
//                                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
//                                    System.out.println("返回结果是：" + resultInfo);
//                                    //开通成功去把isOpenNotify由1变成0
//                                    UserboundPaymentOrder uOrder = new UserboundPaymentOrder();
//                                    uOrder.setIsOpenNotify("0");
//                                    uOrder.setOrderId(responseObj.getBody().getOrderId());
//                                    userboundPaymentOrderService.updateIsOpenNotify(uOrder);
//                                    System.out.println("isOpenNotify由1变成0成功" + uOrder.getOrderId());
//
//                                    //3,现在去数据库更新isopen这个字段
//                                    UnifiedOrderPackages user = new UnifiedOrderPackages();
//                                    user.setOrderId(user.getOrderId());
//                                    user.setOrderStartDate(user.getOrderStartDate());
//                                    user.setOrderExpireDate(user.getOrderExpireDate());
//                                    user.setIsOpen("1");
//                                    user.setUpdateTime(curtime);
//                                    unifiedOrderPackagesService.updateIsOpenByorderId(user);
//                                    System.out.println("UnifiedOrderPackages成功" + user.getOrderId());
//
//                                } else {
//                                    System.out.println("返回结果是：" + resultInfo);
//
//                                }
//
//                            }
//
//
//                        }
//
//
//                    }
//                }
//
//            }
//
//
//            //请求参数处理
////                        OpenMonthlyPackageRequestBody body = new OpenMonthlyPackageRequestBody();
////                        body.setDeviceType(deviceType);
////                        body.setOrderId(orderId);
////
////                        // 请求参数拼接
////                        OpenMonthlyPackageRequest request = new OpenMonthlyPackageRequest();
////                        TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
////                        RequestHeader header = utils.getRequestHeader(Constant.cmd_openMonthlyPackage, partnerCode);
////                        request.setHeader(header);
////                        request.setBody(body);
////                        String sign = utils.getSign(header, body, body.getClass(), secret);
////                        request.setSign(sign);
////
////
////                        // 将参数转换成XML格式
////                        String sRequest = new JaxbUtil(OpenMonthlyPackageRequest.class).toXml(request, "utf-8");
////                        System.out.println("请求报文：" + sRequest);
////
////                        // 发送post请求，并接收返回结果
////                        String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
////                        System.out.println("响应报文：" + result);
////                        OpenMonthlyPackageResponse responseObj = new JaxbUtil(OpenMonthlyPackageResponse.class).fromXml(result, "utf-8");
////                        String retCode = responseObj.getHeader().getResultCode();
////                        String resultInfo = responseObj.getHeader().getResultInfo();
////
////                        if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
////                            System.out.println("返回结果是：" + resultInfo);
////                            //开通成功去把isOpenNotify由1变成0
////                            UserboundPaymentOrder uOrder = new UserboundPaymentOrder();
////                            uOrder.setIsOpenNotify("0");
////                            uOrder.setOrderId(responseObj.getBody().getOrderId());
////                            userboundPaymentOrderService.updateIsOpenNotify(uOrder);
////                            System.out.println("isOpenNotify由1变成0成功" + uOrder.getOrderId());
////
////                            //3,现在去数据库更新isopen这个字段
////                            UnifiedOrderPackages user = new UnifiedOrderPackages();
////                            user.setOrderId(user.getOrderId());
////                            user.setOrderStartDate(user.getOrderStartDate());
////                            user.setOrderExpireDate(user.getOrderExpireDate());
////                            user.setIsOpen("1");
////                            user.setUpdateTime(curtime);
////                            unifiedOrderPackagesService.updateIsOpenByorderId(user);
////                            System.out.println("UnifiedOrderPackages成功" + user.getOrderId());
////
////                        } else {
////                            System.out.println("返回结果是：" + resultInfo);
////
////                        }
////
////                    }
////
////
////                }
//
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}
